﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyShopThoiTrang.QuanLyShopThoiTrang_BUS_WS;

namespace QuanLyShopThoiTrang
{
    public partial class QuanLyHoaDonBanHang : Form
    {
        QLSTT_BUSSoapClient ws = new QLSTT_BUSSoapClient();
        public QuanLyHoaDonBanHang()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            HoaDonBanHang temp = new HoaDonBanHang();
            temp.MaHoaDon = -1;
            List<HoaDonBanHang> listHDBH = ws.HoaDonBanHang_TraCuu(temp);

            dataGridView1.Rows.Clear();
            foreach(HoaDonBanHang hdbh in listHDBH)
            {
                NhanVien nvtemp = new NhanVien();
                nvtemp.MaNhanVien = (int)hdbh.MaNhanVien;

                nvtemp = ws.NhanVien_LayNV(nvtemp);
                dataGridView1.Rows.Add(dataGridView1.Rows.Count + 1, nvtemp.NhanVien_TenDayDu, hdbh.HDBH_NgayLap, hdbh.HDBH_ThanhTien, hdbh.HDBH_KhuyenMai);
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Tag = hdbh;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell cell = dataGridView1.SelectedCells[0];

            DataGridViewRow row = dataGridView1.Rows[cell.RowIndex];

            HoaDonBanHang hdbh = (HoaDonBanHang)row.Tag;

            List<ChiTietHoaDonBanHang> listCTHDBH = ws.ChiTietHoaDonBanHang_TraCuu(hdbh);

            dataGridView2.Rows.Clear();

            foreach(ChiTietHoaDonBanHang cthdbh in listCTHDBH)
            {
                SanPham sptemp = new SanPham();
                sptemp.MaSanPham = (int)cthdbh.MaSanPham;

                sptemp = ws.SanPham_LaySPTheoMaSP(sptemp);
                dataGridView2.Rows.Add(dataGridView2.Rows.Count + 1, sptemp.SanPham_MaHang, sptemp.SanPham_Ten, cthdbh.CTHDBH_SoLuong, cthdbh.CTHDBH_DonGia);
                dataGridView2.Rows[dataGridView2.Rows.Count - 1].Tag = cthdbh;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn muốn xóa những chi tiết đã chọn?", "Xác nhận", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                foreach (DataGridViewCell cell in dataGridView2.SelectedCells)
                {
                    if (0 <= cell.RowIndex && cell.RowIndex < dataGridView2.Rows.Count)
                    {
                        DataGridViewRow row = dataGridView2.Rows[cell.RowIndex];
                        ChiTietHoaDonBanHang cthdbh = (ChiTietHoaDonBanHang)row.Tag;

                        ws.ChiTietHoaDonBanHang_Xoa(cthdbh);
                        dataGridView2.Rows.RemoveAt(row.Index);
                    }
                }

                foreach (DataGridViewRow row in dataGridView2.Rows)
                {
                    row.SetValues(row.Index + 1);
                }
            }
            else
            {
                return;
            }
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn muốn xóa những hóa đơn đã chọn?", "Xác nhận", MessageBoxButtons.YesNoCancel);
            if (dr == DialogResult.Yes)
            {
                foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
                {
                    if (0 <= cell.RowIndex && cell.RowIndex < dataGridView1.Rows.Count)
                    {
                        DataGridViewRow row = dataGridView1.Rows[cell.RowIndex];
                        HoaDonBanHang hdbh = (HoaDonBanHang)row.Tag;

                        List<ChiTietHoaDonBanHang> listCTHDBH = ws.ChiTietHoaDonBanHang_TraCuu(hdbh);
                        foreach(ChiTietHoaDonBanHang cthdbh in listCTHDBH)
                        {
                            ws.ChiTietHoaDonBanHang_Xoa(cthdbh);
                        }

                        ws.HoaDonBanHang_Xoa(hdbh);
                        dataGridView1.Rows.RemoveAt(row.Index);
                    }
                }

                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    row.SetValues(row.Index + 1);
                }

                MessageBox.Show("Thành công");
            }
            else
            {
                return;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            TraCuuHoaDonBanHang tchdbh = new TraCuuHoaDonBanHang();
            tchdbh.Set(this);
            tchdbh.ShowDialog();
        }

        public void TraCuu(HoaDonBanHang hdbhx)
        {
            List<HoaDonBanHang> listHDBH = ws.HoaDonBanHang_TraCuu(hdbhx);

            dataGridView1.Rows.Clear();
            foreach (HoaDonBanHang hdbh in listHDBH)
            {
                NhanVien nvtemp = new NhanVien();
                nvtemp.MaNhanVien = (int)hdbh.MaNhanVien;

                nvtemp = ws.NhanVien_LayNV(nvtemp);
                dataGridView1.Rows.Add(dataGridView1.Rows.Count + 1, nvtemp.NhanVien_TenDayDu, hdbh.HDBH_NgayLap, hdbh.HDBH_ThanhTien, hdbh.HDBH_KhuyenMai);
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Tag = hdbh;
            }
        }

        private void QuanLyHoaDonBanHang_Load(object sender, EventArgs e)
        {

        }
    }
}
